package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketGoodsVo;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.db.domain.MarketOrderVo;
import com.cskaoyan.market.service.AdminStatService;
import com.cskaoyan.market.service.impl.AdminStatServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@WebServlet("/admin/stat/*")
public class AdminStatController extends HttpServlet {

    AdminStatService adminStatService = new AdminStatServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/admin/stat/", "");
        switch (replace) {
            case "order":
                order(req,resp);
                break;
            case "goods":
                goods(req,resp);
                break;
            case "user":
                user(req,resp);
                break;
        }
    }

    private void user(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, List<Object>> mallMap = adminStatService.user();
        Object o = ResponseUtil.ok(mallMap);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<MarketOrderVo> marketOrderList = adminStatService.order();
        Object o = ResponseUtil.okColumn(marketOrderList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void goods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<MarketGoodsVo> marketGoodsList = adminStatService.goods();
        Object o = ResponseUtil.okGoods(marketGoodsList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
